﻿#include "experimentinfowindow.h"
#include "ui_experimentinfowindow.h"
extern intListener doorStatus;
experimentInfoWindow::experimentInfoWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::experimentInfoWindow)
{
    ui->setupUi(this);
    mpShadeWindow = new QWidget(this);

#ifdef Q_OS_ANDROID
    int fontId = QFontDatabase::addApplicationFont(":/FontStyle/AlibabaPuHuiTi-3-55-Regular.ttf");
    if (fontId != -1) {
        QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);
        if (!fontFamilies.isEmpty()) {
            QString familyName = fontFamilies.first();
            QFont font(familyName, 18); // 创建一个QFont对象，设置字体名称和默认大小

            // 使用setFont方法设置字体，传入QFont对象
            ui->ExperimentName->setFont(font);
            ui->AmplProgram->setFont(font);
            ui->Code->setFont(font);
            ui->System_2->setFont(font);
            ui->System->setFont(font);
            ui->System_3->setFont(font);
            ui->label->setFont(font);
            ui->label_2->setFont(font);
            ui->label_3->setFont(font);
            ui->label_4->setFont(font);
            ui->label_5->setFont(font);
            ui->label_7->setFont(font);
            ui->pushButton_5->setFont(font);


            // 如果需要为其他按钮设置不同的大小，可以创建新的QFont对象
            QFont smallerFont(familyName, 16);
            ui->label_8->setFont(smallerFont);
            ui->pushButton->setFont(smallerFont);
            ui->pushButton_4->setFont(smallerFont);
            ui->pushButton_2->setFont(smallerFont);
            ui->pushButton_3->setFont(smallerFont);

        }
    }
#endif
    QRegExp regExp("^[\u4E00-\u9FA5a-zA-Z0-9_]+$");
    QRegExpValidator *validator = new QRegExpValidator(regExp, this);
    ui->ExperimentName->setValidator(validator);
    ui->ExperimentName->setMaxLength(30);

    ui->Code->setValidator(validator);
    ui->Code->setMaxLength(30);
    refShadeWindow();
    connect(&doorStatus, &intListener::valueChanged,this,&experimentInfoWindow::doorStatusChanged,Qt::QueuedConnection);
}
void experimentInfoWindow::doorStatusChanged(){
    if(doorStatus.get()==0)
    {
        ui->pushButton_3->setText("        打开仓门");
    }
    else
    {
        ui->pushButton_3->setText("        关闭仓门");
    }
}

experimentInfoWindow::~experimentInfoWindow()
{
    delete ui;
}

void experimentInfoWindow::showEvent(QShowEvent *event)
{
    tcpClient=TcpClient::getInstance();

    ui->label->setText("<font color=red>*</font><font>实验名称</font>");
    ui->label_2->setText("<font color=red>*</font><font>扩增程序</font>");

    ui->label_8->hide();

    ui->System->blockSignals(true);
    ui->System->clear();
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select Name from ItemCode Where DicCode='4'";
    query.exec(sql);
    while (query.next()) {
        ui->System->addItem(query.value("Name").toString());
    }

    ui->System->blockSignals(false);

    ui->System->blockSignals(false);

    ui->System_2->blockSignals(true);
    ui->System_2->clear();

    sql="Select Name from ItemCode Where DicCode='12' order by IsDefault desc";
    query.exec(sql);
    while (query.next()) {
        ui->System_2->addItem(query.value("Name").toString());
    }

    ui->System_2->blockSignals(false);

    ui->System_3->blockSignals(true);
    ui->System_3->clear();
    sql="Select Name from ItemCode Where DicCode='6'";
    query.exec(sql);
    while (query.next()) {
        ui->System_3->addItem(query.value("Name").toString());
    }

    ui->System_3->blockSignals(false);


    if(doorStatus.get()==0){
        ui->pushButton_3->setText("        打开仓门");
    }else {
        ui->pushButton_3->setText("        关闭仓门");
    }

    if(MainWindow::experimentStatus.get()==4){
        ui->pushButton_9->show();
        ui->pushButton_10->show();

        ui->Code->setEnabled(true);
        ui->System->setEnabled(true);
        ui->System_2->setEnabled(true);
        ui->System_3->setEnabled(true);
        ui->AmplProgram->setEnabled(true);
        ui->ExperimentName->setEnabled(true);

        ui->AmplProgram->blockSignals(true);
        ui->AmplProgram->clear();
        sql="Select programName from AmplProgramTemp order by isDefault desc";

        query.exec(sql);
        while (query.next()) {
            ui->AmplProgram->addItem(query.value("ProgramName").toString());
        }

        ui->AmplProgram->blockSignals(false);


        ui->pushButton->setEnabled(false);
        ui->pushButton_3->setEnabled(false);
        ui->pushButton_4->setEnabled(false);

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType,TestKit "
            "From ExperimentInfo e "
            "Left Join AmplProgramTemp a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
            ui->System_3->setCurrentText(query.value("ConsumableType").toString());
            ui->System_2->setCurrentText(query.value("TestKit").toString());
        }
    }
    else if(MainWindow::experimentStatus.get()==2){
        ui->pushButton_9->show();
        ui->pushButton_10->show();

        ui->Code->setEnabled(false);
        ui->System->setEnabled(false);
        ui->System_2->setEnabled(false);
        ui->System_3->setEnabled(false);
        ui->AmplProgram->setEnabled(false);
        ui->ExperimentName->setEnabled(false);

        ui->AmplProgram->blockSignals(true);
        ui->AmplProgram->clear();
        sql="Select programName from AmplProgram order by isDefault desc";

        query.exec(sql);
        while (query.next()) {
            ui->AmplProgram->addItem(query.value("ProgramName").toString());
        }

        ui->AmplProgram->blockSignals(false);

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType,TestKit "
            "From ExperimentInfo e "
            "Left Join AmplProgram a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
            ui->System_2->setCurrentText(query.value("TestKit").toString());
            ui->System_3->setCurrentText(query.value("ConsumableType").toString());
        }
    }
    else if(MainWindow::experimentStatus.get()==1){
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        ui->Code->setEnabled(false);
        ui->System->setEnabled(false);
        ui->System_2->setEnabled(false);
        ui->System_3->setEnabled(false);
        ui->AmplProgram->setEnabled(false);
        ui->ExperimentName->setEnabled(false);

        ui->AmplProgram->blockSignals(true);
        ui->AmplProgram->clear();
        sql="Select programName from AmplProgram order by isDefault desc";

        query.exec(sql);
        while (query.next()) {
            ui->AmplProgram->addItem(query.value("ProgramName").toString());
        }

        ui->AmplProgram->blockSignals(false);

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType,TestKit "
            "From ExperimentInfo e "
            "Left Join AmplProgram a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
            ui->System_2->setCurrentText(query.value("TestKit").toString());
            ui->System_3->setCurrentText(query.value("ConsumableType").toString());
        }


    }
    else if(MainWindow::experimentStatus.get()==5){
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        ui->Code->setEnabled(true);
        ui->System->setEnabled(true);
        ui->System_2->setEnabled(true);
        ui->System_3->setEnabled(true);
        ui->AmplProgram->setEnabled(true);
        ui->ExperimentName->setEnabled(true);

        ui->AmplProgram->blockSignals(true);
        ui->AmplProgram->clear();
        ui->AmplProgram->addItem("");

        sql="Select programName from AmplProgram order by isDefault desc";

        query.exec(sql);
        while (query.next()) {
            ui->AmplProgram->addItem(query.value("ProgramName").toString());
        }

        ui->AmplProgram->blockSignals(false);

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType,TestKit "
            "From ExperimentInfo e "
            "Left Join AmplProgramTemp a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
            ui->System_2->setCurrentText(query.value("TestKit").toString());
            ui->System_3->setCurrentText(query.value("ConsumableType").toString());
        }
    }
    else{
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_3->setEnabled(true);
        ui->pushButton_4->setEnabled(true);

        ui->Code->setEnabled(true);
        ui->System->setEnabled(true);
        ui->System_2->setEnabled(true);
        ui->System_3->setEnabled(true);
        ui->AmplProgram->setEnabled(true);
        ui->ExperimentName->setEnabled(true);

        sql="Select Name from ItemCode where DicCode='4' and IsDefault='1'";
        query.exec(sql);
        while (query.next()) {
            ui->System->setCurrentText(query.value("Name").toString());
        }


        sql="Select Name from ItemCode where DicCode='6' and IsDefault='1'";
        query.exec(sql);
        while (query.next()) {
            ui->System_3->setCurrentText(query.value("Name").toString());
        }

        MainWindow::experimentGuid = common->GetGuid();
        sql=QString("Delete from ExperimentInfo");
        query.exec(sql);
        query.clear();

        ui->AmplProgram->blockSignals(true);
        ui->AmplProgram->clear();
        sql="Select programName from AmplProgram order by isDefault desc";

        query.exec(sql);
        while (query.next()) {
            ui->AmplProgram->addItem(query.value("ProgramName").toString());
        }

        ui->AmplProgram->blockSignals(false);

        QDateTime dateTime = QDateTime::currentDateTime();
        QString experimentName=QString::number(dateTime.date().year())+GetDateString(dateTime.date().month())+GetDateString(dateTime.date().day())+GetDateString(dateTime.time().hour())+GetDateString(dateTime.time().minute())+GetDateString(dateTime.time().second());
        sql=QString("Insert into ExperimentInfo values ("
                    "'%1','%2',(select id from AmplProgram where IsDefault='True'),'','','','未启动','','','')").arg(MainWindow::experimentGuid,experimentName);
        query.exec(sql);
        query.clear();

        ui->ExperimentName->setText(experimentName);
        ui->Code->setText("");
    }
    refShadeWindow();
}

void experimentInfoWindow::resizeEvent(QResizeEvent *event){
    refShadeWindow();
}

void experimentInfoWindow::refShadeWindow(){
    if(mpShadeWindow){
        mpShadeWindow->close();
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    bool isFirst=false;
    int guideStatus;

    QString sql="Select * From IsFirstStartUp";
    query.exec(sql);
    while (query.next()) {
        isFirst = query.value("IsFirst").toString()=="0";
        guideStatus = query.value("GuideStatus").toInt();
    }
    query.clear();

    if(isFirst){
        if(guideStatus==4)//扩增程序
        {
            mpShadeWindow = new QWidget(this);
            QString str("QWidget{background-color:rgba(0,0,0,0.6);}");   //0.6代表透明度，根据需要自行调整
            mpShadeWindow->setStyleSheet(str);
            QRect rect = this->geometry();
            int screenWidth = rect.width();  // 屏幕宽度
            int screenHeight = rect.height();  // 屏幕高度
            mpShadeWindow->setGeometry(0, 0,screenWidth,screenHeight);    //遮罩窗口位置

            QRect btnRect= ui->pushButton->frameGeometry();

            QLabel *label=new QLabel("4/4新建实验",mpShadeWindow);
            QPalette palette;
            palette.setColor(QPalette::WindowText, QColor(255,255,255));
            QFont font=label->font();
            font.setPointSize(24);
            label->setFont(font);
            label->setPalette(palette);
            label->setGeometry(btnRect.x()-40,screenHeight-160,180,30);
            label->setStyleSheet("background-color:rgba(0,0,0,0);");

            QPushButton * btnSkipThis = new QPushButton("跳过当前引导",mpShadeWindow);
            btnSkipThis->setGeometry(btnRect.x()-110,screenHeight-120,120,40);
            btnSkipThis->setStyleSheet("border-radius: 8px; background-color:#FFFFFF");

            QObject::connect(btnSkipThis, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set GuideStatus=5";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton * btnSkipAll = new QPushButton("跳过全部引导",mpShadeWindow);
            btnSkipAll->setGeometry(btnRect.x()+20,screenHeight-120,120,40);
            btnSkipAll->setStyleSheet("border-radius: 8px; background-color:#2F42CD");

            QObject::connect(btnSkipAll, &QPushButton::clicked,[this](){
                QSqlDatabase db = dbHelper::openConnection();
                QSqlQuery query(db);

                QString sql="Update IsFirstStartUp set IsFirst='1',GuideStatus=5";
                query.exec(sql);
                query.clear();

                refShadeWindow();
            });

            QPushButton *btnSet=new QPushButton("下一步",mpShadeWindow);
            btnSet->setGeometry(btnRect.x(),screenHeight-60,120,40);
            QFont btnFont=btnSet->font();
            font.setPointSize(12);
            btnSet->setFont(font);
            btnSet->setStyleSheet("background-color: qlineargradient(x1: 0, y0: 0, x2: 0, y2: 1, stop: 0 #8C97ED, stop: 1 #0018D1);border-radius: 8px; color: white;");

            QLabel *movieLabel=new QLabel("",mpShadeWindow);
            movieLabel->setGeometry(btnRect.x(),screenHeight-40,60,60);
            movieLabel->setStyleSheet("background-color:rgba(0,0,0,0);");

            QMovie  *guideMovie = new QMovie(":/pic/picture/guide.gif");
            movieLabel->setMovie(guideMovie);
            guideMovie->start();

            QObject::connect(btnSet, &QPushButton::clicked,[this](){
                on_pushButton_clicked();
            });

            mpShadeWindow->show();
        }
    }
}


QString experimentInfoWindow::GetDateString(int val){
    return QString::number(val).length()==2?QString::number(val): "0"+QString::number(val);
}

void experimentInfoWindow::on_pushButton_clicked()
{
    if(ui->AmplProgram->currentText().isNull() ||ui->AmplProgram->currentText().isEmpty()){
        QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "请先创建扩增程序", QMessageBox::Yes, this);
        box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
        box->exec();
        return;
    }
    if(!ui->ExperimentName->text().isEmpty()){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString experimentName=ui->ExperimentName->text();
        QString code=ui->Code->text();
        QString programName=ui->AmplProgram->currentText();
        QString system=ui->System->currentText();
        QString ConsumableType=ui->System_3->currentText();
        QString testKit=ui->System_2->currentText();
        QString sql=QString("Update ExperimentInfo set ExperimentName='%1', "
                            "code='%2',"
                            "AmplGuid=(select id from AmplProgram where programName='%3'),"
                            "System='%4',"
                            "ConsumableType='%5',"
                            "TestKit='%6' "
                            "where id='%7'").arg(experimentName,code,programName,system,ConsumableType,testKit,MainWindow::experimentGuid);

        query.exec(sql);
        query.clear();

        if(MainWindow::experimentStatus.get()==0){

            db.transaction();
            sql=QString("Delete from SampleInfo");
            query.exec(sql);

            sql=QString("Delete from SampleAccessInfo");
            query.exec(sql);

            for(int i=0;i<12;i++){
                for(int j=0;j<8;j++){
                    QString sampleId=common->GetGuid();
                    sql=QString("Insert into SampleInfo values("
                                "'%1',"
                                "'%2',"
                                "'%3',"
                                "'%4',"
                                "'',"
                                "'',"
                                "'','%5')").arg(sampleId,MainWindow::experimentGuid,QString::number(i),QString::number(j),QString::number(i*8+j+1));
                    query.exec(sql);
                    query.clear();

                }
            }
            db.commit();
        }
        this->close();
        emit changeSampleWindow();
    }
}

void experimentInfoWindow::on_pushButton_2_clicked()
{
    this->close();
    if(MainWindow::experimentStatus.get() == 5){
        MainWindow::experimentStatus.set(0);
    }
    emit changeMainWindow();
}

void experimentInfoWindow::on_pushButton_4_clicked()
{
    this->close();
    emit changeMainWindow();
}

void experimentInfoWindow::on_ExperimentName_textChanged(const QString &arg1)
{
    if(arg1.isEmpty()){
        ui->label_8->show();
    }else {
        ui->label_8->hide();
    }
}

void experimentInfoWindow::on_pushButton_9_clicked()
{
    this->close();
    emit changeDataAnalysisWindow();
}

void experimentInfoWindow::on_pushButton_10_clicked()
{
    this->close();
    emit changeSampleWindow();
}

void experimentInfoWindow::on_pushButton_3_clicked()
{
    if(doorStatus.get()==0)
    {
        doorStatus.set(1);
        ui->pushButton_3->setText("        关闭仓门");
    }
    else
    {
        doorStatus.set(0);
        ui->pushButton_3->setText("        打开仓门");
    }
}

void experimentInfoWindow::on_pushButton_5_clicked()
{
    if(ui->AmplProgram->currentText().isNull() ||ui->AmplProgram->currentText().isEmpty()){
        QMessageBox *box = new QMessageBox(QMessageBox::Question, "提示", "请先创建扩增程序", QMessageBox::Yes, this);
        box->button(QMessageBox::Yes)->setText("确认");// 将原本显示“Yes”的按钮改为显示“确认”
        box->exec();
        return;
    }
    QString id;
    QString sql=QString("Select ID From AmplProgram where ProgramName='%1'").arg(ui->AmplProgram->currentText());

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    query.exec(sql);
    while(query.next()){
        id=query.value("ID").toString();
    }
    this->close();
    emit changeAmplProgramStepWindow(id);
}

